<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>

    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加分类</el-button>
        </el-col>
      </el-row>

<!--      表格-->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        class="treeTable"
        border
        :show-row-hover="true"
      >
<!--        是否有效-->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted ===false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>

        </template>
<!--排序-->
        <template slot="order" slot-scope="scope">

          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>

<!--        操作-->

        <template slot="opt" slot-scope="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" >删除</el-button>

        </template>

      </tree-table>


<!--      分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[3, 5, 10, 15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>

<!--    添加分类的对话框-->

  </div>
</template>

<script>
export default {
name: "Cate",
  data() {
    return {
      queryInfo:{
        type:3,
        pagenum: 1,
        pagesize:5
      },
      cateList:[],

      total:0,

      columns:[
          {label:'分类名称',prop:'cat_name'},
          {label: '是否有效',
            //将当前页定义成模板列
            type:'template',
            //表示当前这一列使用的模板名称
            template:'isok'
          },
        {label: '排序',
          //将当前页定义成模板列
          type:'template',
          //表示当前这一列使用的模板名称
          template:'order'
        },

        {label: '操作',
          //将当前页定义成模板列
          type:'template',
          //表示当前这一列使用的模板名称
          template:'opt'
        }
          ]
    }
  },
  created() {
    this.getCateList()
  },
  methods :{
    async getCateList() {
       const {data:res}=await this.$http.get('categories',{params:this.queryInfo})

      if(res.meta.status !==200) {
          return this.$message.error('获取商品分类失败')
      }
      console.log(res.data,"datatata")
      this.cateList=res.data.result
      this.total=res.data.total
    },

    //监听 pagesize 改变
    handleSizeChange(newSize) {
        this.queryInfo.pagesize=newSize;
        this.getCateList();
    },
    //监听pagenum的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum=newPage
      this.getCateList()
    }


  }
}
</script>

<style scoped>
  .treeTable {
    margin-top: 15px;
  }
</style>
